#include "cmsis_os2.h"
#include "ohos_init.h"
#include "roarlang.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "los_event.h"
#include "los_swtmr.h"
#include "los_task.h"
#include "iot_gpio.h"
#include "iot_i2c.h"
#include "hi_io.h"
#include "hi_pwm.h"
// #include "hi_boot_rom.h"
// #include "hi_watchdog.h"

// static void swtmr_updateOBVM(UINT32 para) { updateOpenBlockVM(); }
// UINT16 swtmr_obvmupid;
// static void startOpenBlockUpdate() {
//   LOS_SwtmrCreate(1, LOS_SWTMR_MODE_PERIOD, swtmr_updateOBVM, &swtmr_obvmupid,
//                   1);
//   LOS_SwtmrStart(swtmr_obvmupid);
// }
static UINT32 openBlockUpdateTask(VOID){
  printf("startOpenBlockVM\n");
  startOpenBlockVM();
  while(true){
    osDelay(1);
    updateOpenBlockVM();
    // hi_watchdog_feed();
  }
  return 0;
}

UINT32 ret;
TSK_INIT_PARAM_S taskParam1 = {0};
UINT32 g_taskId;
static void startOpenBlockUpdate1() {

    /* 锁任务调度，防止新创建的任务比本任务高而发生调度 */
    LOS_TaskLock();

    printf("LOS_TaskLock() Success!\n");

    taskParam1.pfnTaskEntry = (TSK_ENTRY_FUNC)openBlockUpdateTask;
    taskParam1.usTaskPrio = 4;
    taskParam1.pcName = "RoarLang Task";
    taskParam1.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
    // taskParam1.uwResved = LOS_TASK_ATTR_JOINABLE; /* detach 属性 */

    /* 创建高优先级任务，由于锁任务调度，任务创建成功后不会马上执行 */
    ret = LOS_TaskCreate(&g_taskId, &taskParam1);
    if (ret != LOS_OK) {
      printf("Example_TaskHi create Failed!\n");
    }else{
      printf("Example_TaskHi create Success!\n");
    }
    LOS_TaskUnlock();

}
static void MainEntry(void) {
  // printf('enable watchdog\n');
  // hi_watchdog_enable();
  // printf('start watchdog\n');
  hi_watchdog_set_timeout(6500);
  startOpenBlockUpdate1();
  // while(1){
  // 	updateOpenBlockVM();
  // 	osDelay(2);
  // }
  return;
}
SYS_RUN(MainEntry);